package com.atguigu.demo.gmall.controller;

import com.atguigu.demo.gmall.bean.OrderInfo;
import com.atguigu.demo.gmall.service.OrderInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单表 订单表 前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2023-06-27
 */
@RestController
public class OrderInfoController {


    @Autowired
    OrderInfoService orderInfoService;

    //  条件查询
    @GetMapping("/orders")
    public List<OrderInfo> getOrderList(@RequestParam(value = "status",required = false) String orderStatus,
                                        @RequestParam(value = "skuName" ,required = false) String skuName){
        List<OrderInfo> orderInfoList = orderInfoService.list(new QueryWrapper<OrderInfo>().eq(orderStatus != null, "order_status", orderStatus)
                .like(skuName != null, "trade_body", skuName));
        return orderInfoList;
    }


    //  主键查询
    @GetMapping("/order/{id}")
    public  OrderInfo getOrderInfo(@PathVariable("id")String id){
        OrderInfo orderInfo = orderInfoService.getById(id);
        return  orderInfo;
    }


    //  新增
    @PostMapping("/order")
    public String saveOrder(@RequestBody OrderInfo orderInfo){
        orderInfoService.saveOrUpdate(orderInfo);
        return "success";
    }

    //  修改  根据主键  修改某一个字段
    @PostMapping("/orderstatus/{id}")
    public String  updateStatus(@PathVariable("id") String orderId ,@RequestBody OrderInfo orderInfo){

        orderInfoService.update(new UpdateWrapper<OrderInfo>().eq("id",orderId).set("order_status",orderInfo.getOrderStatus()));
        return "success";
    }

    //  删除  根据主键
    @DeleteMapping("/order/{id}")
    public String deleteOrder(@PathVariable("id") String orderId){

        orderInfoService.removeById(orderId);
        return "success";
    }

}
